var Direction = {
	listInit: function(){ 
		$ajax({
			url: "/direction/list",
			success: function(res) {
				var html = '<table class="table table-bordered"><thead><tr>'+
                            '<th class="span4">IT方向</th>'+  
                            '<th class="span6">描述</th>'+
                            '<th class="span2">操作</th>'+
	                    '</tr></thead><tbody>';

	            var items = res.result.results;
	            for (var i=0; i<items.length; i++) {
	            	var btnHtml = '<button class="btn btn-sm updateBtn">修改</button> <button class="btn btn-danger btn-sm removeBtn">删除</button>';
	            	html += '<tr data-pos="'+ i +'">'+
	            				'<td>'+ items[i].name +'</td>'+   
	            				'<td class="umw15">'+ items[i].desc +'</td>'+
	            				'<td>'+ btnHtml +'</td>'+
	            			'</tr>';

	            }
	            html += '</tbody></table>';
	            $("#listCtn").html(html);
	            $(".removeBtn").click(function() {
	            	var item = items[$(this).parents("tr").data("pos")];
		        	if(window.confirm('你确定要删除IT方向“' +  item.name + '”吗？删除后将不能恢复！')){
		            	$ajax({
		            		url:"/direction/remove",
		            		data:{_id:item._id},
		            		success: function() {
		            			window.location.href = "/directionManage.html";
		            		}
		            	});
		          	}	            	
	            });
	           	$(".updateBtn").click(function() {
	            	var item = items[$(this).parents("tr").data("pos")];
	            	window.location.href = "/directionCreate.html?id="+item._id;
	            });
			}
		});	            
	},

	createInit: function() {
		$("#create").click(Direction.add);

		var id = getValue('id');
		if(id) {
			$("#title").text('修改IT方向');
			$("#courseList").show();
			$("#projectList").show();
			$ajax({
				url: "/direction/detail",
				data: {
					_id: id,
				},
				success: function(res) {
					var direction = res.result;
					$("#name").val(direction.name);
					$("#desc").val(direction.desc);
					
					SetLS("direcdom","projectBottom");
					var length = direction.projects && direction.projects.length || 0;
					for(var i=0; i<length; i++) 
						Direction.addTopDom(direction.projects[i]._id, direction.projects[i].name);

					SetLS("direcdom","courseBottom");
					length = direction.courses && direction.courses.length || 0;
					for(var i=0; i<length; i++) 
						Direction.addTopDom(direction.courses[i]._id, direction.courses[i].name);
				}
			});	
			$("#courseTop").click(function(){
				SetLS("direcdom","courseBottom");
				$ajax({
					url: "/course/list",
					data: {
						status: STATUS.PASS,
					},					
					success: function(res) {
						var items = res.result.results;
						var length = items && items.length || 0;
						var courseHtml = '<select id="courseSel">';
						for (var i=0; i<length; i++) 
							courseHtml += '<option value="' + items[i]._id + '">' + items[i].name + '</option>';
						courseHtml += '</select>';
						ShowCover("加入课程","课程名称","","Direction.addTopDom()",courseHtml);						
					}
				});
			});	
			$("#projectTop").click(function(){
				SetLS("direcdom","projectBottom");
				$ajax({
					url: "/project/list",
					data: {
						status: STATUS.PASS,
					},						
					success: function(res) {
						var items = res.result.results;
						var length = items && items.length || 0;
						var projectHtml = '<select id="courseSel">';
						for (var i=0; i<length; i++) 
							projectHtml += '<option value="' + items[i]._id + '">' + items[i].name + '</option>';
						projectHtml += '</select>';
						ShowCover("加入项目","项目名称","","Direction.addTopDom()",projectHtml);						
					}
				});	
			});		
		}else { 
			$("#title").text('新建IT方向');
			$("#courseList").hide();
			$("#projectList").hide();
		}
	},
	topMove: function(){ 
		var $this = $(event.currentTarget);
		var thisEvt = $this.parent().parent();
		var thisHtml = thisEvt.prop('outerHTML');
		var prevHtml = thisEvt.prev().prop('outerHTML');
		if (prevHtml){ 
			thisEvt.prev().before(thisHtml);
			thisEvt.remove();
		} else 
			$alert("再也不能向上了");
	},
	bottomMove: function(){ 
		var $this = $(event.currentTarget);
		var thisEvt = $this.parent().parent();
		var thisHtml = thisEvt.prop('outerHTML');
		var nextHtml = thisEvt.next().prop('outerHTML');
		if (nextHtml){ 
			thisEvt.next().after(thisHtml);
			thisEvt.remove();
		} else 
			$alert("再也不能向下了");
	},
	domRemove: function(){
		var $this = $(event.currentTarget);
		var thisEvt = $this.parent().parent();
		thisEvt.remove();
	},
	addTopDom: function(id, name){ 
		// var name = $("#full-name").val();
		var id = id?id:$("#cover option:selected").val();
		var text = name?name:$("#cover option:selected").text();
		var directdom = GetLS("direcdom");
		$("#cover").html("");
		var childdom = 	"<div class='box-toolbox box-toolbox-top' style='margin:-10px;padding:10px 5px;'>" + 
                        "    <div class='pull pull-left'>" + 
                        "        <div data-id='" + id + "' class='title up5 uf16'>" + text + "</div>" + 
                        "    </div>" + 
                        "    <div class='pull pull-right'>" + 
                        "        <a class='btn btn-white up' onclick='Direction.topMove();' style='padding: 2px 6px;'>" + 
                        "            <i class='icon-arrow-up'></i>" + 
                        "        </a>" + 
                        "        <a class='btn btn-white down' onclick='Direction.bottomMove();' style='padding: 2px 6px;'>" + 
                        "            <i class='icon-arrow-down'></i>" + 
                        "        </a>" + 
                        "        <a class='btn btn-white down' onclick='Direction.domRemove();' style='padding: 2px 6px;'>" + 
                        "            <i class='icon-remove'></i>" + 
                        "        </a>" + 
                        "    </div>" + 
                        "</div>";
        var flag = true;
        $("#" + directdom).find(".title").each(function(i){
        	if (id == $(this).data("id"))
        		flag = false;
        });                
        if (flag)
			$("#" + directdom).append(childdom);
		else
			return $alert("您已添加过此内容");
	},
	add: function() {
		var name = $("#name").val();
		var desc = $("#desc").val();
		if (name == "" || desc == "") 
			return $alert("IT方向名称和描述不能为空");

		var id = getValue("id");
		var data = {};
		data.name = name;
		data.desc = desc;
		if(id)
			data._id = id;
		data.courses = new Array();
		$("#courseBottom").find(".title").each(function(i){
			data.courses[i] = new Object();
			data.courses[i].id = $(this).data("id");
			data.courses[i].name = $(this).text();
		});
		data.projects = new Array();
		$("#projectBottom").find(".title").each(function(i){
			data.projects[i] = new Object();
			data.projects[i].id = $(this).data("id");
			data.projects[i].name = $(this).text();
		});
		$ajax({
			url: "/direction/upsert",
			data: data,
			success: function(res) {
				window.location.href = "/directionManage.html";
			}
		});
	},
}
